package com.ask.forMe.langchain4j.config;

import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

@Configuration
public class RAGConfig {

    @Autowired
    private EmbeddingModel embeddingModel;
    @Autowired
    private EmbeddingStore embeddingStore;


    @Bean
    ContentRetriever contentRetrieverXiaozhi() {
        // 从指定路径加载文档
        Document document1 = FileSystemDocumentLoader.loadDocument("C:/knowledge/商城信息/商品信息.txt");
        Document document2 = FileSystemDocumentLoader.loadDocument("C:/knowledge/商城信息/商城信息.md");
        List<Document> documents = Arrays.asList(document1, document2);

        // 创建向量嵌入的存储容器
        InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();

        // 将文档内容转换为 文本段落嵌入向量
        EmbeddingStoreIngestor.ingest(documents, embeddingStore);

        // 从嵌入存储（EmbeddingStore）里检索和查询内容相关的信息
        return EmbeddingStoreContentRetriever.from(embeddingStore);
    }


    /**
     * 配置词向量检索器，指定嵌入模型和嵌入存储数据库
     *
     * @return
     */
    @Bean
    ContentRetriever contentRetrieverXiaozhiPincone() {
        return EmbeddingStoreContentRetriever.builder()
                .embeddingModel(embeddingModel)
                .embeddingStore(embeddingStore)
                .maxResults(1)
                .minScore(0.8)
                .build();
    }
}
